<!--
* @description:采购申请
* @fileName: PurchasingRequisition
* @author:孟帅龙
* @date:2021-1-14
* @后台人员:孟帅龙
-->
<template>
  <div>
    <!-- 采购申请 -->
    <div class="cgsq">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="编号：" prop="PurchaseApplyNumber">
          <el-input
            v-model="ruleForm.PurchaseApplyNumber"
            placeholder="编号的格式为cg+4位数字"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="货物名称：" prop="GoodsName">
          <el-input
            v-model="ruleForm.GoodsName"
            placeholder="请输入货物名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型：" prop="Typey">
          <el-select v-model="ruleForm.Typey" placeholder="请选择类型">
            <el-option
              v-for="item in type"
              :key="item.value"
              :label="item.DictionaryName"
              :value="item.Id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材质：" prop="Texture">
          <el-select v-model="ruleForm.Texture" placeholder="请选择材质">
            <el-option
              v-for="item in caizhi"
              :key="item.value"
              :label="item.DictionaryName"
              :value="item.Id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格：" prop="Specification">
          <el-input
            v-model="ruleForm.Specification"
            placeholder="请输入规格"
          ></el-input>
        </el-form-item>
        <el-form-item label="产地:">
          <el-col :span="11">
            <el-form-item prop="Nationw">
              <el-select
                v-model="ruleForm.Nationw"
                placeholder="请选择国家"
                @change="loadProvince"
              >
                <el-option
                  v-for="item in country"
                  :key="item.value"
                  :label="item.CountryName"
                  :value="item.Id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="Province">
              <el-select v-model="ruleForm.Province" placeholder="请选择省/州">
                <el-option
                  v-for="item in province"
                  :key="item.value"
                  :label="item.CountryName"
                  :value="item.Id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="数量" prop="Numbers">
          <el-input
            v-model="ruleForm.Numbers"
            placeholder="请输入数值"
          ></el-input>
        </el-form-item>
        <el-form-item label="期望交付日期：" prop="ExpectDeliveryTime">
          <el-date-picker
            type="date"
            placeholder="请选择期望交付日期"
            v-model="ruleForm.ExpectDeliveryTime"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="用途描述" prop="UseDescribes">
          <el-input
            type="textarea"
            v-model="ruleForm.UseDescribes"
            placeholder="请输入用途描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="Remarkm">
          <el-input
            type="textarea"
            v-model="ruleForm.Remarkm"
            placeholder="请输入备注信息"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交审核</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="loadQuXiao">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      caizhi: [],
      type: [],
      country: [],
      province: [],
      ruleForm: {
        PurchaseApplyNumber: "",
        GoodsName: "",
        Typey: "",
        Texture: "",
        Specification: "",
        Nationw: "",
        Province: "",
        Numbers: "",
        ExpectDeliveryTime: "",
        UseDescribes: "",
        Remarkm: "",
      },
      rules: {
        PurchaseApplyNumber: [
          { required: true, message: "请输入编号", trigger: "blur" },
          { min: 6, max: 6, message: "长度为6个字符", trigger: "blur" },
        ],
        GoodsName: [
          { required: true, message: "请输入货物名称", trigger: "blur" },
        ],
        Typey: [{ required: true, message: "请选择类型", trigger: "change" }],
        Texture: [{ required: true, message: "请选择材质", trigger: "change" }],
        Specification: [
          { required: true, message: "请输入规格", trigger: "blur" },
        ],
        Nationw: [{ required: true, message: "请选择国家", trigger: "change" }],
        Province: [
          { required: true, message: "请选择省份", trigger: "change" },
        ],
        Numbers: [{ required: true, message: "请输入数量", trigger: "blur" }],
        ExpectDeliveryTime: [
          {
            type: "date",
            required: true,
            message: "请选择期望交付日期",
            trigger: "change",
          },
        ],
        UseDescribes: [
          { required: true, message: "请填写用途描述", trigger: "blur" },
        ],
        Remarkm: [{ required: true, message: "请填写备注", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          this.$axios
            .post(
              "https://localhost:44399/api/app/purchase-request/add-purchase-request",
              this.ruleForm
            )
            .then((res) => {
              // var num = /^\d+(\.\d+)?&/; //匹配数字
              // if (!num.test(this.ruleForm.Numbers)) {
              //   this.$message.error("数量只能是数字");
              //   return;
              // }
              if (res.data > 0) {
                this.$message.success("添加成功");
                this.loadQuXiao();
              } else {
                this.$message.success("添加失败");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadQuXiao() {
      this.$router.push("/MaterialsPurchasing");
    },
    loadDictionary() {
      this.$axios
        .get("https://localhost:44399/api/app/oil-cost/bind")
        .then((res) => {
          let d = res.data.PageData;
          this.caizhi = d.filter((u) => {
            return u.DictionaryType == "Texture";
          });
          this.type = d.filter((u) => {
            return u.DictionaryType == "Typey";
          });
        });
    },
    loadCountry() {
      this.$axios
        .get("https://localhost:44399/api/app/country/0/country-show")
        .then((res) => {
          this.country = res.data.PageData;
        });
    },
    loadProvince() {
      this.$axios
        .get(
          "https://localhost:44399/api/app/country/" +
            this.ruleForm.Nationw +
            "/country-show"
        )
        .then((res) => {
          this.province = res.data.PageData;
        });
    },
  },
  created() {
    this.loadDictionary();
    this.loadCountry();
  },
};
</script>

<style>
.cgsq {
  width: 500px;
  margin: 50px;
}
</style>